<template>
    <!-- element-plus中的容器 -->
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside>
            <!-- 菜单顶部logo -->
            <div class="el-aside-logo" @click="router.push('/layout')">
                <img class="logoimg" src="@/assets/logo.svg" alt="">
                <span class="logospan">
                    谢勇的网站
                </span>
            </div>
            <!-- element-plus的菜单标签 -->
            <el-menu style="border-right: none;" router :default-active="router.currentRoute.value.path"
                :default-openeds="[]">
                <el-menu-item index="/home">
                    <el-icon>
                        <HomeFilled />
                    </el-icon>
                    <span>首页</span>
                </el-menu-item>
                <el-sub-menu index="article">
                    <template #title>
                        <el-icon>
                            <Document />
                        </el-icon>
                        <span>文章</span>
                    </template>
                    <el-menu-item-group>
                        <el-menu-item index="/article/manager">
                            <el-icon>
                                <Document />
                            </el-icon>
                            <span>文章管理</span>
                        </el-menu-item>
                        <el-menu-item index="/article/category">
                            <el-icon>
                                <Reading />
                            </el-icon>
                            <span>文章分类</span>
                        </el-menu-item>
                    </el-menu-item-group>
                </el-sub-menu>
                <el-sub-menu index="account" v-if="userStore.imAdmin()">
                    <template #title>
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>用户管理</span>
                    </template>
                    <el-menu-item-group>
                        <el-menu-item index="/admin" v-if="userStore.imSuperAdmin()">
                            <el-icon>
                                <Avatar />
                            </el-icon>
                            <span>管理员信息</span>
                        </el-menu-item>
                        <el-menu-item index="/user">
                            <el-icon>
                                <UserFilled />
                            </el-icon>
                            <span>普通用户信息</span>
                        </el-menu-item>
                    </el-menu-item-group>
                </el-sub-menu>
                <el-sub-menu index="info" v-if="userStore.imAdmin()">
                    <template #title>
                        <el-icon>
                            <Monitor />
                        </el-icon>
                        <span>信息管理</span>
                    </template>
                    <el-menu-item-group>
                        <el-menu-item index="/info/notice">
                            <el-icon>
                                <Notification />
                            </el-icon>
                            <span>系统公告</span>
                        </el-menu-item>
                        <!-- <el-menu-item index="/blog">博客管理</el-menu-item> -->
                        <!-- <el-menu-item index="/dict">字典管理</el-menu-item> -->
                    </el-menu-item-group>
                </el-sub-menu>
                <el-sub-menu index="future" v-if="userStore.imAdmin()">
                    <template #title>
                        <el-icon>
                            <Loading />
                        </el-icon>
                        <span>开发中……</span>
                    </template>
                    <el-menu-item-group>
                        <el-menu-item index="/data">
                            <el-icon>
                                <Loading />
                            </el-icon>
                            <span>数据统计</span>
                        </el-menu-item>
                    </el-menu-item-group>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧容器 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <!-- 显示路由的区域 -->
                <div> {{ router.currentRoute.value.meta.name }} </div>
                <!-- 头像的区域 -->
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <div class="el-dropdown__box">
                        <el-image class="avatar" v-if="data.user?.avatar" :src="data.user?.avatar" />
                        <el-image class="avatar" v-else :src="getConfig('DEFAULT_USER_LOGO')" />
                        <div class="nickname">{{ data.user?.nickname }}</div>
                    </div>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <!-- command事件，条目被点击后触发，且在事件函数上可以声明一个参数，接收条目对应的指令 -->
                            <el-dropdown-item command="person" :icon="User">个人中心</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <el-main>
                <RouterView />
            </el-main>
            <el-footer>
                learning @2025 Created by 谢勇
            </el-footer>
        </el-container>
    </el-container>
</template>

<script setup>
import router from '@/router';
import { ref } from 'vue';
import { getConfig } from '@/utils/config';
import { User, SwitchButton } from '@element-plus/icons-vue';
import { useUserStore } from '@/store/user';
import { useTokenStore } from '@/store/token';
import { ElMessage, ElMessageBox } from 'element-plus';

const userStore = useUserStore()
const tokenStore = useTokenStore()
const data = ref({
    user: userStore.getUser()
});

const handleCommand = (command) => {
    // 下拉框条目被点击后调用的函数
    if(command === 'logout') {
        // 退出登录
        ElMessageBox.confirm(
            '您确定要退出吗？',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        ).then( async () => {
            userStore.$reset
            tokenStore.removeToken()
            router.push('/login')
            ElMessage.success('退出登录成功')
        }).catch(() => {
            ElMessage({
                type: 'info',
                message: '取消退出登录',
            })
        })
    } else {
        // 打开个人中心页面
        router.push('/' + command)
    }
}

</script>

<style scoped>
:deep(.el-menu-item-group__title) {
    padding: 1px;
}

:deep(.el-menu--inline .el-menu-item) {
    padding-left: 60px;
}

:deep(.el-dropdown) {
    cursor: pointer;
}

:deep(.el-tooltip__trigger) {
    outline: none;
}

.layout-container {
    height: 100vh;

    .el-aside {
        width: 200px;
        border-right: 1px solid #eee;


        .el-menu .is-active {
            /* 设置高亮样式 */
            background-color: var(--el-menu-hover-bg-color);
            color: #303133;
        }

        .el-aside-logo {
            height: 60px;
            display: flex;
            align-items: center;
            padding-left: 15px;
            cursor: pointer;

            .logoimg {
                width: 40px;
                height: 40px;
            }

            .logospan {
                font-size: 20px;
                font-weight: bold;
                color: #409EFF;
                margin-left: 15px;
            }
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #eee;

        .el-dropdown__box {
            display: flex;
            align-items: center;
            margin-right: 10px;

            .avatar {
                width: 40px;
                height: 40px;
                border-radius: 50%;
                margin-right: 15px;
            }

            .nickname {
                font-size: 18px;
                font-weight: 600;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-main {
        padding: 0px;
    }

    .el-footer {
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #666;
        background-color: #f2f4ff;
    }
}
</style>